using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using IReaper.FileData;
using IReaper.FileUtils;
using IReaper.PathManager;

namespace IReaper.Statues
{
    public partial class TransferFileWork : AsyncWork
    {
        #region fields
        CourseFileData[] files;
        OperationType operation;
        bool overwrite;
        string rootPath;
        PathPolicyType policy;
        FileTransferDelegate onTransfered;
        int processedFileNumber;
        string taskMessageRoot;
        #endregion

        public TransferFileWork(CourseFileData[] files,
                                OperationType operation,
                                bool overwrite,
                                string rootPath,
                                PathPolicyType policy)
                                : this()
        {
            //copy all the parameters.
            this.files = files;
            this.operation = operation;
            this.overwrite = overwrite;
            this.rootPath = rootPath;
            this.policy = policy;
            //create delegate
            this.onTransfered = this.OnFileTransfered;
            //init other param
            this.processedFileNumber = 0;
            if (operation == OperationType.Copy)
            {
                this.taskMessageRoot = IReaper.Properties.Resources.FileTransferMessageRootCopy;
            }
            else
            {
                this.taskMessageRoot = IReaper.Properties.Resources.FileTransferMessageRootMove;
            }
        }

        public TransferFileWork()
        {
            InitializeComponent();
        }

        public TransferFileWork(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        protected override void OnDoWork(DoWorkEventArgs e)
        {
            base.OnDoWork(e);
            //invoke the execution
            try
            {
                FileUtils.FileTransferManager.TransferCourseFileData(files,
                                                                        policy,
                                                                        overwrite,
                                                                        rootPath,
                                                                        operation,
                                                                        this.onTransfered);
                if (this.operation == OperationType.Copy)
                {
                    CourseFileDataManager.GenerateRecordFile(files, System.IO.Path.Combine(rootPath, ".export"));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            finally
            {
                IReaper.Running.DownloadEngine.ResumeDownloadEngine();
            }
        }

        

        private void OnFileTransfered(CourseFileData File, string srcPath, string desPath)
        {
            this.Status.TaskMessage = this.taskMessageRoot + srcPath;
            this.processedFileNumber += 1;
            this.Status.Percentage = (int)(this.processedFileNumber * 100 / files.Length);
        }
    }
}
